package com.huaguoguo.cyg.service.tl.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaguoguo.cyg.dao.tl.TlGangMapper;
import com.huaguoguo.cyg.entity.tl.dto.RankExtDTO;
import com.huaguoguo.cyg.entity.tl.dto.TlGangDTO;
import com.huaguoguo.cyg.entity.tl.qo.GangQO;
import com.huaguoguo.cyg.entity.tl.vo.GangVO;
import com.huaguoguo.cyg.service.tl.data.ITlGangService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author huaguoguo
 * @since 2020-02-23
 */
@Service
public class TlGangServiceImpl extends ServiceImpl<TlGangMapper, TlGangDTO> implements ITlGangService {

    /**
     * 获取日期区间内的帮派排行
     * @return
     */
    @Override
    public Map<String, List<RankExtDTO>> getGangListByDate(List<GangQO> gangQOList){
        QueryWrapper<TlGangDTO> queryWrapper = new QueryWrapper<>();
        List<RankExtDTO> gangExtDTOList = baseMapper.getGangRank(gangQOList);
        return gangExtDTOList.stream().map(ext -> {
            ext.setRemark(String.join(":",ext.getAreaName(),ext.getGangName()));
            return ext;
        }).collect(Collectors.groupingBy(RankExtDTO::getRemark));
    }

    /**
     * 获取当日指定取的帮会列表
     * @param areaId
     * @return
     */
    @Override
    public List<GangVO> getByAreaId(Integer areaId) {
        QueryWrapper<TlGangDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_id",areaId)
                .eq("update_date", LocalDate.now());
        List<TlGangDTO> tlGangDTOList = baseMapper.selectList(queryWrapper);
        return tlGangDTOList.stream().map(gang -> BeanUtil.toBean(gang,GangVO.class))
                .collect(Collectors.toList());
    }
}
